package com.af.community.smart.business

import android.os.Bundle
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.getString
import com.af.community.smart.base.gone
import com.af.community.smart.base.oneClick
import com.af.community.smart.fragment.OnFragmentListener
import com.af.community.smart.fragment.PeopleDetail1Fragment
import com.af.community.smart.fragment.PeopleDetail2Fragment
import com.af.community.smart.model.PeopleData
import org.jetbrains.anko.frameLayout
import org.jetbrains.anko.startActivity

class PeopleDetailActivity : BaseActivity(), OnFragmentListener {

    private lateinit var peopleType: String
    private lateinit var mData: PeopleData

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        frameLayout { id = R.id.people_container }
        initTitle(title = "详情", name = "编辑")
    }

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        peopleType = intent.getStringExtra("type")
        mData = intent.getSerializableExtra("data") as PeopleData
        if (mData.gridId != getString("gridId")) tvRight?.gone()

        when (peopleType) {
            "1" -> {
                supportFragmentManager.beginTransaction()
                    .add(
                        R.id.people_container,
                        PeopleDetail1Fragment().apply {
                            arguments = Bundle().apply { putSerializable("data", mData) }
                        }
                    )
                    .commit()
            }
            "2" -> {
                supportFragmentManager.beginTransaction()
                    .add(
                        R.id.people_container,
                        PeopleDetail2Fragment().apply {
                            arguments = Bundle().apply { putSerializable("data", mData) }
                        }
                    )
                    .commit()
            }
        }

        tvRight?.oneClick {
            when (peopleType) {
                "1" -> startActivity<PeopleEditFirstActivity>("data" to mData)
                "2" -> startActivity<PeopleEditSecondActivity>("data" to mData)
            }
        }
    }

    override fun onDataChanged(item: Any?) {
        if (item !== null && item is PeopleData) mData = item
    }

}
